<template>
  <div id="confirm"></div>
</template>

<script setup lang="ts">
import { useDialog } from 'naive-ui';
import { getCurrentInstance } from 'vue';
import { ConfirmType } from './type'
const confirmDialog = useDialog();

const confirmWarning = ({ title, content, positiveText, negativeText }: ConfirmType = {}): Promise<any> => {
  return new Promise((resolve: (value?: any) => void, reject: (value?: any) => void) => {
    confirmDialog.warning({
      title: title ?? '警告',
      content: content ?? '是否删除该数据？',
      positiveText: positiveText ?? '确定',
      negativeText: negativeText ?? '取消',
      onPositiveClick: (): void => {
        resolve()
      },
      onNegativeClick: (): void => {
        reject()
      }
    })
  })
}

const confirmSuccess = ({ title, content, positiveText, negativeText }: ConfirmType = {}): Promise<any> => {
  return new Promise((resolve: (value?: any) => void, reject: (value?: any) => void) => {
    confirmDialog.success({
      title: title ?? '通知',
      content: content ?? '你已提交成功！',
      positiveText: positiveText ?? '确定',
      negativeText: negativeText ?? '取消',
      onPositiveClick: (): void => {
        resolve()
      },
      onNegativeClick: (): void => {
        reject()
      }
    })
  })
}

const confirmError = ({ title, content, positiveText, negativeText }: ConfirmType = {}): Promise<any> => {
  return new Promise((resolve: (value?: any) => void, reject: (value?: any) => void) => {
    confirmDialog.error({
      title: title ?? '警告',
      content: content ?? '发生错误',
      positiveText: positiveText ?? '确定',
      negativeText: negativeText ?? '取消',
      onPositiveClick: (): void => {
        resolve()
      },
      onNegativeClick: (): void => {
        reject()
      }
    })
  })
}

const instance = getCurrentInstance();
const emit = defineEmits(['register'])
instance && emit('register', { confirmWarning,confirmSuccess,confirmError});

</script>

<style scoped>

</style>
